import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

// 引入element-ui插件
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

// 引入配置过的axios插件
import axios from './plugins/axios';

Vue.use(ElementUI);
Vue.config.productionTip = false;

// 以插件的形式使用：this.axios
// 可以直接调用，不用重新导入，减少代码量
Vue.prototype.axios = axios;

// 全局函数
Vue.prototype.playSong = function (id) {
  this.$router.push({ path: '/song', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playMv = function (id) {
  this.$router.push({ path: '/mv', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playVideo = function (id) {
  this.$router.push({ path: '/video', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playPlaylistDetail = function (id) {
  this.$router.push({ path: '/playlist', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playPlaylists = function (cat) {
  this.$router.push({ path: '/playlists', query: { cat: cat } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playAlbumDetail = function (id) {
  this.$router.push({ path: '/album', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playArtistDetail = function (id) {
  this.$router.push({ path: '/artist', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playUserDetail = function (id) {
  this.$router.push({ path: '/user', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playUserEvent = function (id) {
  this.$router.push({ path: '/user/event', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playUserFollows = function (id) {
  this.$router.push({ path: '/user/follows', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playUserFans = function (id) {
  this.$router.push({ path: '/user/fans', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playMy = function () {
  this.$router.push({ path: '/my' });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playMyPlaylist = function (id) {
  // let query = { id: id };
  // console.log('pp', id);
  this.$router.push({ path: '/my/playlist', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playMyArtists = function () {
  this.$router.push({ path: '/my/artists' });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playMyFollows = function () {
  this.$router.push({ path: '/my/follows' });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playMyFans = function () {
  this.$router.push({ path: '/my/fans' });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playMyVideos = function (id) {
  this.$router.push({ path: '/my/videos' });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playMyEdit = function (id) {
  this.$router.push({ path: '/my/edit', query: { id: id } });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playFriend = function () {
  this.$router.push({ path: '/friend' });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.playSearch = function (k, type) {
  this.$router.push({
    path: '/search',
    query: { k: k, type: type },
  });
  document.documentElement.scrollTo({ top: 0, behavior: 'smooth' });
};
Vue.prototype.addSongs = function (item, all = false) {
  if (!window.localStorage) {
    console.log('浏览器不支持localStorage');
  } else {
    var s = localStorage.getItem('songLists');
    if (s) {
      // 有歌曲时 先获取歌曲列表，然后判断准备添加的歌曲是否已经存在
      var s2 = JSON.parse(s);
      if (!all) {
        // 单曲
        var hk = false;
        for (let song of s2) {
          if (item.id === song.id) {
            hk = true;
            break;
          }
        }
        if (!hk) {
          s2.push(item);
          s2 = JSON.stringify(s2);
          localStorage.setItem('songLists', s2);
          // this.$store.state.addSong = true;
          this.$store.commit('changeAddSong', true);
        }
      } else {
        // 全部
        var newItem = [];
        var i = 0;
        for (let asong of item) {
          i = 0;
          for (let song of s2) {
            if (song.id == asong.id) {
              break;
            }
            i++;
          }
          if (i == s2.length) {
            newItem[newItem.length] = asong;
          }
        }
        // console.log(newItem);
        s2.push(...newItem);
        s2 = JSON.stringify(s2);
        localStorage.setItem('songLists', s2);
        // this.$store.state.addSong = true;
        this.$store.commit('changeAddSong', true);
      }
      // this.$store.state.haveSong = true;
      this.$store.commit('changeHaveSong', true);
    } else {
      // 没有歌曲时
      if (!all) {
        // 单曲
        var s1 = [item];
        s1 = JSON.stringify(s1);
        localStorage.setItem('songLists', s1);
        // this.$store.state.addSong = true;
        this.$store.commit('changeAddSong', true);
        // this.$store.state.haveSong = true;
        this.$store.commit('changeHaveSong', true);
      } else {
        // 全部
        var s1 = [...item];
        s1 = JSON.stringify(s1);
        localStorage.setItem('songLists', s1);
        // this.$store.state.addSong = true;
        this.$store.commit('changeAddSong', true);
        // this.$store.state.haveSong = true;
        this.$store.commit('changeHaveSong', true);
      }
    }
  }
};
Vue.prototype.playSongs = function (item, all = false) {
  if (!window.localStorage) {
    console.log('浏览器不支持localStorage');
  } else {
    var s = localStorage.getItem('songLists');
    if (s) {
      // 有歌曲时 先获取歌曲列表，然后判断准备添加的歌曲是否已经存在
      var s2 = JSON.parse(s);
      if (!all) {
        // 单曲
        var hk = false;
        for (let [index, song] of s2.entries()) {
          if (item.id === song.id) {
            hk = true;
            // this.$store.state.lastLength = index;
            this.$store.commit('changeLastLength', index);
            break;
          }
        }
        if (!hk) {
          // 歌单里没有时，添加到最后面，然后开始播放
          // this.$store.state.lastLength = s2.length;
          this.$store.commit('changeLastLength', s2.length);

          s2.push(item);
          s2 = JSON.stringify(s2);
          localStorage.setItem('songLists', s2);
          // this.$store.state.clickPlaySong = true;
          this.$store.commit('changeClickPlaySong', true);
        } else {
          // this.$store.state.clickPlaySong = true;
          this.$store.commit('changeClickPlaySong', true);
        }
      } else {
        var newItem = [];
        var i = 0;
        var hk = false;
        for (let [ai, asong] of item.entries()) {
          i = 0;
          for (let [index, song] of s2.entries()) {
            if (song.id == asong.id) {
              if (ai == 0) {
                hk = true;
                // this.$store.state.lastLength = index;
                this.$store.commit('changeLastLength', index);
              }
              break;
            }
            i++;
          }
          if (i == s2.length) {
            newItem[newItem.length] = asong;
          }
        }
        if (!hk) {
          // this.$store.state.lastLength = s2.length;
          this.$store.commit('changeLastLength', s2.length);
        }
        s2.push(...newItem);
        s2 = JSON.stringify(s2);
        localStorage.setItem('songLists', s2);
        // this.$store.state.clickPlaySong = true;
        this.$store.commit('changeClickPlaySong', true);
      }
    } else {
      // 没有歌曲时
      if (!all) {
        var s1 = [item];
        // this.$store.state.lastLength = 0;
        this.$store.commit('changeLastLength', 0);
        s1 = JSON.stringify(s1);
        localStorage.setItem('songLists', s1);
        // this.$store.state.clickPlaySong = true;
        this.$store.commit('changeClickPlaySong', true);
      } else {
        var s1 = [...item];
        // this.$store.state.lastLength = 0;
        this.$store.commit('changeLastLength', 0);
        s1 = JSON.stringify(s1);
        localStorage.setItem('songLists', s1);
        // this.$store.state.clickPlaySong = true;
        this.$store.commit('changeClickPlaySong', true);
      }
    }
  }
};
Vue.prototype.changeStoreCode = function (code) {
  this.$store.commit('changeCode', code);
};

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app');
